define [
	'jquery'
	'sammy/plugins/sammy.template-0.7.1.min.js'
	'jquery.couch.js'
	'helper'
], ($, undef, undef, H) ->
	$.fn.mb_view_receipt = (S) ->
			$this = $(@)
			$.subscribe "mb.donation", (e, {id: _dnID}) ->
				$.couch.db("vcn-members").openDoc _dnID, {
					success: (donation) ->
						$.couch.db("vcn-members").openDoc donation["donor_id"], {
							success: (donor) ->
							
								d = (new Date()).toDateString().split(' ')
								_date = "#{d[2]}-#{d[1]}-#{d[3].slice(2)}"
								
								pd = donation.donation_date.split(' ')[0].split('-')
								nd = new Date()
								nd.setUTCFullYear(pd[0])
								nd.setUTCMonth(Number(pd[1]) - 1)
								nd.setUTCDate(pd[2])
								nd = nd.toDateString().split(' ')
								_ndate = "#{nd[2]}-#{nd[1]}-#{nd[3].slice(2)}"
								
								if donor.org_name?
									donor.received = donor.org_name
								else 
									donor.received = "#{donor.last_name}, #{donor.first_name}".toCaps()
								
								_fields = [	'tel_home', 
											'province', 
											'city', 
											'tel_work', 
											'tel_fax', 
											'org_name', 
											'tel_cell', 
											'postal_code', 
											'street_address', 
											'last_name', 
											'first_name']
								
								donor[i] or= "" for i in _fields
								
								donor.province = ", #{donor.province}" if donor.province isnt ""
							
								$this.empty().append("
									<div>
										<div style='float: right; display: table;'> 
											Receipt printed on #{_date} 
										</div>
										<div >
											<h3> Vancouver Community Network Association </h3>
										</div>
								
									</div>
									<div style='display: table; position: relative; left: 50px;'> 
										411 Dunsmuir St.
										<br>
										Vancouver, B.C.
										<br>
										V6B 1X4
										<br>
										(604) 257-3811
									</div>

									<p>
										<b>* This receipt is NOT for tax deduction or credit purposes. *</b>
									</p>
									<hr>
							
									<table>
										<tbody>
											<tr>
												<td class='label front'>Received: </td>
												<td colspan ='3'> #{donor.received} </td>
											</tr>
											<tr>
												<td class='label front'> From: </td>
												<td colspan = '3'> #{donor.street_address.toCaps()} </td>
											</tr>
											<tr>
												<td></td>
												<td colspan = '3'>#{donor.city.toCaps()}#{donor.province.toCaps()}</td>
											</tr>
											<tr>
												<td></td>
												<td colspan = '3'> #{donor.postal_code.toUpperCase()} </td>
											</tr>
											<tr>
												<td class='label front'> For: </td>
												<td> #{donation.donation_type.toCaps()} </td>
												<td class='label' style='width: 1px;'> Amount: </td>
												<td> $ #{donation.donation_amount}.00</td>
											</tr>
										</tbody>
									</table>
									<hr>
									Payment Received on #{_ndate}
								")
								$this.find('table')
									.css('width', '100%')
								$('.label')
									.css('font-weight', 'bold')
									.css('text-align', 'right')
								$('.label.front')
									.css('width', '1px')
								$('td:not(.label)')
									.css('padding-left', '5px')
							}
					
					}
		return @
	return
